package joaomarceloos.scribblerapp;

import joaomarceloos.scribblerapp.client.PaintActivity;
import joaomarceloos.scribblerapp.utils.IPAdrressUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	Button btnCreateGroup, btnEnterGroup;
	EditText editIP, editName;
	String[] painterName, painterAddr;
	int rank;

	private OnClickListener createGroupListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			startPaintActivity(true);
		}
	};
	
	private OnClickListener enterGroupListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			startPaintActivity(false);
		}
	};
	
	private void startPaintActivity(boolean isServer){
		Intent i = new Intent(MainActivity.this, PaintActivity.class);
		
		painterName = new String[2];
		painterName[0] = "Joao";
		painterName[1] = "Maria";
		
		painterAddr = new String[2];
		if (isServer) {
			painterAddr[0] = IPAdrressUtils.getIPAddress(true);
			painterAddr[1] = editIP.getText().toString();
			rank = 0;
		} else {
			painterAddr[0] = editIP.getText().toString();
			painterAddr[1] = IPAdrressUtils.getIPAddress(true);
			rank = 1;
		}
		
		i.putExtra("painterName", painterName);
		i.putExtra("painterAddr", painterAddr);
		i.putExtra("rank", rank);
		startActivity(i);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnCreateGroup = (Button)(findViewById(R.id.btnCreateGroup));
		btnCreateGroup.setOnClickListener(createGroupListener);
		
		btnEnterGroup = (Button)(findViewById(R.id.btnEnterGroup));
		btnEnterGroup.setOnClickListener(enterGroupListener);

		editName = (EditText)(findViewById(R.id.editName));
		editName.setText("Nome");
		
		editIP = (EditText)(findViewById(R.id.editIP));
		editIP.setText("192.168.1.");
		editIP.setSelection(editIP.getText().length());
	}

}
